package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.ConditionVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.enums.ConditionEnum;
import com.itheima.sfbx.insurance.mapper.ConditionMapper;
import com.itheima.sfbx.insurance.pojo.*;
import com.itheima.sfbx.insurance.service.ICategoryConditionService;
import com.itheima.sfbx.insurance.service.IConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
/**
 * @Description：筛选项服务实现类
 */
@Slf4j
@Service
public class ConditionServiceImpl extends ServiceImpl<ConditionMapper, Condition> implements IConditionService {



    @Override
    public Page<ConditionVO> findPage(ConditionVO conditionVO, int pageNum, int pageSize) {
        Page<Condition> page = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(conditionVO.getConditionKey()), Condition::getConditionKey, conditionVO.getConditionKey())
                .like(StrUtil.isNotBlank(conditionVO.getConditionKeyName()), Condition::getConditionKeyName, conditionVO.getConditionKeyName())
                .eq(StrUtil.isNotBlank(conditionVO.getDataState()), Condition::getDataState, conditionVO.getDataState())
                .page(new Page<>(pageNum, pageSize));
        return BeanConv.toPage(page, ConditionVO.class);
    }

    @Override
    public ConditionVO findById(String conditionId) {
        //TODO
        return null;
    }

    @Autowired
    private ICategoryConditionService categoryConditionService;

    @Autowired
    private IInsuranceConditionService insuranceConditionService;

    @Override
    @Transactional
    public ConditionVO save(ConditionVO conditionVO) {
        Integer count = this.lambdaQuery().eq(Condition::getConditionKey, conditionVO.getConditionKey()).count();
        if (count > 0) {
            log.error("筛选项存在，删除失败:{}", conditionVO.getConditionKey());
            throw new ProjectException(ConditionEnum.SAVE_FAIL);
        }
        Integer count1 = categoryConditionService.lambdaQuery()
                .eq(CategoryCondition::getConditionKey, conditionVO.getConditionKey())
                .count();
        Integer count2 = insuranceConditionService.lambdaQuery()
                .eq(InsuranceCondition::getConditionKey, conditionVO.getConditionKey())
                .count();
        if (count1>0 || count2>0) {
            throw new ProjectException(ConditionEnum.SAVE_FAIL);
        }
        Condition bean = BeanConv.toBean(conditionVO, Condition.class);
        boolean save = this.save(bean);
        if (!save){
            throw new ProjectException(ConditionEnum.SAVE_FAIL);
        }
        return BeanConv.toBean(bean, ConditionVO.class);
    }

    @Override
    @Transactional
    public Boolean update(ConditionVO conditionVO) {
        Condition condition = this.getById(conditionVO);
        if (ObjectUtil.isEmpty(condition)){
            log.error("筛选项不存在，更新失败:{}", condition.getConditionKey());
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        Integer count = this.lambdaQuery().eq(Condition::getConditionKey, condition.getConditionKey())
                .ne(Condition::getId, condition.getId())
                .count();
        if (count > 0) {
            log.error("修改筛选项失败，因为key已经存在:{}", condition.getId());
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        Integer count1 = categoryConditionService.lambdaQuery()
                .eq(CategoryCondition::getConditionKey, condition.getConditionKey())
                .count();
        Integer count2 = insuranceConditionService.lambdaQuery()
                .eq(InsuranceCondition::getConditionKey, condition.getConditionKey())
                .count();
        if (count1>0 || count2>0){
            log.error("筛选项关联绑定有关系，更新失败：{}", condition.getConditionKey());
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        boolean b = this.updateById(BeanConv.toBean(conditionVO, Condition.class));
        if (!b){
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        for (String checkedId : checkedIds) {
            Condition condition = this.getById(checkedId);
            if (ObjectUtil.isEmpty(condition)){
                log.error("筛选项不存在，删除失败:{}", checkedId);
                throw new ProjectException(CoefficentEnum.DEL_FAIL);
            }
            Integer count1 = categoryConditionService.lambdaQuery()
                    .eq(CategoryCondition::getConditionKey, condition.getConditionKey())
                    .count();
            Integer count2 = insuranceConditionService.lambdaQuery()
                    .eq(InsuranceCondition::getConditionKey, condition.getConditionKey())
                    .count();
            if (count1>0 || count2>0){
                log.error("筛选项关联绑定有关系，删除失败：{}", condition.getConditionKey());
                throw new ProjectException(CoefficentEnum.DEL_FAIL);
            }
        }
        return this.removeByIds(Arrays.asList(checkedIds));
    }

    @Override
    public List<ConditionVO> findList(ConditionVO conditionVO) {
        List<Condition> list = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(conditionVO.getConditionKey()), Condition::getConditionKey, conditionVO.getConditionKey())
                .like(StrUtil.isNotBlank(conditionVO.getConditionKeyName()), Condition::getConditionKeyName, conditionVO.getConditionKeyName())
                .eq(StrUtil.isNotBlank(conditionVO.getDataState()), Condition::getDataState, conditionVO.getDataState())
                .list();
        return BeanConv.toBeanList(list, ConditionVO.class);
    }

    @Override
    public List<ConditionVO> findInConditionKeys(List<String> conditionKeys) {
        //TODO
        return null;
    }
}
